#include "udf_cfg.h"

#if ( ENABLE_UDF16 > 0 && !defined(UDP_SUB_PROJECT) && !defined(UDT_SUB_PROJECT) && (defined(UDF16_SUB_PROJECT) || defined(UDX_SUB_PROJECT)) )
#define APP_DEMO_ENABLE
#include "app.h"


#if 0 // ROM内置中断响应函数
	if( handler_id == SysTick_IRQn ){
		gPara.SysTick_cnt ++;
	}else if( handler_id == USCI1_IRQn ){
		uint32_t reg_isr = 0;
		reg_isr = USCI1->ISR;
		uint8_t data;
		//	SYSC->CLK_SEL&=~LRC_DIV128_MSK; //128K clk
		//RX
		if(reg_isr&USCI_ISR_RXNE_Msk)
		{
			data = USCI1->TRX;
			if(gPara.strUart.RecFinish)
				return;
			gPara.strUart.timeoutStart = 1;
			gPara.strUart.timeoutCnt = 0;

			if(data==0x0A){
				gPara.strUart.RecFinish = 1;
				gPara.strUart.timeoutCnt = 0;
				gPara.strUart.timeoutStart = 0;
			}
			if(gPara.strUart.RecLen<(UART_REC_MAX_LEN-1)){
				gPara.strUart.RecBuff[gPara.strUart.RecLen] = data;
				gPara.strUart.RecLen++;
			}
		}
		//ERR
		if(reg_isr&0x10)
		{
			//CSTD_PRINTF("[CONS]config reg_isr ERROR! %08X\r\n",reg_isr);
			USCI1->ISR=0x10;
		}
		if( !gPara.startup ){
			SYSC->CLK_EN |= WDT_PATTERN_EN_Msk; // ENABLE WDT clock  	
			WDT->CMP = 0;	
			WDT->STR = 0x1;						// bit0:clear
			while(1);
		}
	}else{
		while(1);
	}
#elif DEMO_CFG == DEMO_CFG_APP_UART_CFG

// gPara/p_rom_uart_recv为ROM内置全局变量。
//若需要扩大p_rom_uart_recv指向结构体大小，则应改为使用sUDF_global。
//若变小/不变可强转p_rom_uart_recv指针类型使用。
void udf16_udf_handle( int handler_id )
{
	if( handler_id == USCI1_IRQn ){
		uint32_t reg_isr = 0;
		reg_isr = USCI1->ISR;
		
		if(reg_isr&USCI_ISR_RXNE_Msk)
		{
			uint8_t data = USCI1->TRX;
			if(p_rom_uart_recv->RecFinish)
				return;
			p_rom_uart_recv->timeoutStart = 1;
			p_rom_uart_recv->timeoutCnt = 0;

			if(data==';'){ // '\n'
				p_rom_uart_recv->RecFinish = 1;
				p_rom_uart_recv->timeoutCnt = 0;
				p_rom_uart_recv->timeoutStart = 0;
			}
			if(p_rom_uart_recv->RecLen<(UART_REC_MAX_LEN-1)){
				p_rom_uart_recv->RecBuff[p_rom_uart_recv->RecLen] = data;
				p_rom_uart_recv->RecLen++;
			}
		}
		if(reg_isr&0x10)
		{
			USCI1->ISR=0x10;
		}
		if( !p_rom_gb->startup ){
			SYSC->CLK_EN |= WDT_PATTERN_EN_Msk; // ENABLE WDT clock  	
			WDT->CMP = 0;	
			WDT->STR = 0x1;						// bit0:clear
			while(1);
		}
	}else{
		while(1);
	}
}
#elif DEMO_CFG == DEMO_CFG_APP_TEST_TEMP

// gPara/p_rom_uart_recv为ROM内置全局变量。
//若需要扩大p_rom_uart_recv指向结构体大小，则应改为使用sUDF_global。
//若变小/不变可强转p_rom_uart_recv指针类型使用。
void udf16_udf_handle( int handler_id )
{
	if( handler_id == SysTick_IRQn ){
		sUDF_global.test_para0 ++;
		sUDF_global.test_para1 ++;
		(*p_rom_user_para0) ++;
		(*p_rom_user_para1) ++;
	}else if( handler_id == USCI1_IRQn ){
		uint32_t reg_isr = 0;
		reg_isr = USCI1->ISR;
		
		if(reg_isr&USCI_ISR_RXNE_Msk)
		{
			uint8_t data = USCI1->TRX;
			if(p_rom_uart_recv->RecFinish)
				return;
			p_rom_uart_recv->timeoutStart = 1;
			p_rom_uart_recv->timeoutCnt = 0;

			if(data==';'){ // '\n'
				p_rom_uart_recv->RecFinish = 1;
				p_rom_uart_recv->timeoutCnt = 0;
				p_rom_uart_recv->timeoutStart = 0;
			}
			if(p_rom_uart_recv->RecLen<(UART_REC_MAX_LEN-1)){
				p_rom_uart_recv->RecBuff[p_rom_uart_recv->RecLen] = data;
				p_rom_uart_recv->RecLen++;
			}
		}
		if(reg_isr&0x10)
		{
			USCI1->ISR=0x10;
		}
		if( !p_rom_gb->startup ){
			SYSC->CLK_EN |= WDT_PATTERN_EN_Msk; // ENABLE WDT clock  	
			WDT->CMP = 0;	
			WDT->STR = 0x1;						// bit0:clear
			while(1);
		}
	}else{
		while(1);
	}
}

#else
#error "udf16 no demo"
#endif

#endif
